package ReSofting.it.GoldDirector.Db;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import ReSofting.it.GoldDirector.Model.myCategoria;
import ReSofting.it.GoldDirector.Model.myMenu;
import ReSofting.it.GoldDirector.Model.myNetSettings;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataDbManager 
{
	DatabaseHelper _dbHelper = null;
	Context _Context = null;
	
	public DataDbManager(Context pContext)
	{
		_dbHelper = new DatabaseHelper(pContext); 
		 this._Context = pContext;
			

		try {
			_dbHelper.createDataBase();
		} 
		catch (IOException e) {
			Log.d("ERROR in DataDbManager", e.getMessage());
		}
			
	
		
	
	}//fm
	
	public void CloseDb()
	 {
		_dbHelper.close();
	 }
	
	public String  getInfoTabelleDatiDb()
	{		
		return _dbHelper.getInfoTabelleDatiDb();
	}
	
	
	public void ExecSqlNoRet(String pSql)
	{		
		_dbHelper.execSqlNoRet(pSql);
	}
	
	
	public Boolean addNetSettingsInDb(myNetSettings pNetSet, Boolean pDeleteExistingData)
	{
		try
    	{
			if(pDeleteExistingData)
				_dbHelper.execSqlNoRet("Delete from NetSettings");
			

	       Log.d("In addNetSettingsInDb " , "Start");
	           		
	        ContentValues values = null ;
	        ArrayList<ContentValues> valuesList = new ArrayList<ContentValues>();

	
        	values = new ContentValues();
        	values.put("HostIp",pNetSet.HostIp );
        	values.put("Port", pNetSet.Port );   
        	values.put("ServiceCore", pNetSet.ServiceCore );  
        	values.put("ServiceLogin", pNetSet.ServiceLogin ); 
 	       	valuesList.add(values);
		 	          
	        
	        _dbHelper.insertDataIntoTable("NetSettings", valuesList);
	       
	        return true;
        
    	}
        catch(SQLException sqlerror)
        {
	        Log.v("ERROR in addNetSettingsInDb ", sqlerror.getMessage());	
	        return false;
        }
		
	}//fM
	
	 public myNetSettings getNetSettingsFromDb() {
		 
		 //10.0.2.2:8096
		 myNetSettings result = new myNetSettings("10.0.2.2","8096","","");
		 
		 	try {
		
				//SQLiteDatabase database = getReadableDatabase();
				Cursor cursor = _dbHelper.ExecSql("SELECT * FROM NetSettings" );
				if(cursor == null)
					return result;
				

				cursor.moveToFirst();
				
				while (!cursor.isAfterLast()) 
				{
					result.HostIp =  cursor.getString(0);
				    result.Port =  cursor.getString(1);
				    result.ServiceCore =  cursor.getString(2);
				    result.ServiceLogin =  cursor.getString(3);
				    
				    break;
				    //cursor.moveToNext();
				}
				cursor.close();
      

				return result;
				
				
			} catch (Exception e) {
				 Log.v("ERROR in getNetSettingsFromDb ", e.getMessage());
				e.printStackTrace();
				return result;
			}
	    }
	
	 //*******
	 

	
	

	public Boolean addCategoriesInDb(List<myCategoria> pListDati, Boolean pDeleteExistingData)
	{
		try
    	{
			if(pDeleteExistingData)
				_dbHelper.execSqlNoRet("Delete from Categories");
			
	       if(pListDati == null) return false;
	       Log.d("In addCategories " , "Size : "+ pListDati.size()+"" );
	       
    		myCategoria dato = null;
	        ContentValues values = null ;
	        ArrayList<ContentValues> valuesList = new ArrayList<ContentValues>();

	        for(int i = 0; i< pListDati.size(); i++) 
	        {
	        	dato = pListDati.get(i);
	        	if(dato != null)
	        	{
		        	values = new ContentValues();
		        	values.put("Nome", dato.Nome );
		 	        values.put("AreaProd", dato.AreaProd );
		 	       // values.put("datas", this.getCurrentDateTime().format("%d/%m/%y %T"));
		 	        
		 	       valuesList.add(values);
	        	}	 	        
	        }
	        
	        _dbHelper.insertDataIntoTable("Categories", valuesList);
	       
	        return true;
        
    	}
        catch(SQLException sqlerror)
        {
	        Log.v("ERROR in addCategories ", sqlerror.getMessage());	
	        return false;
        }
		
	}//fM
	


	public Boolean addMenuItemsInDb(List<myMenu> pListDati, Boolean pDeleteExistingData)
	{
		try
    	{
			if(pDeleteExistingData)
				_dbHelper.execSqlNoRet("Delete from MenuItems");
			
			
			if(pListDati == null) return false;
			Log.d("In addMenuItems " , "Size : "+ pListDati.size()+"" );
			
			
			myMenu dato = null;
	        ContentValues values = null ;
	        ArrayList<ContentValues> valuesList = new ArrayList<ContentValues>();

	        for(int i = 0; i< pListDati.size(); i++) 
	        {
	        	dato = pListDati.get(i);
	        	if(dato != null)
	        	{
		        	values = new ContentValues();
		        	values.put("Guid", dato.GuidIDMn.toString() );		 	        
		 	        values.put("Categoria", dato.FkCategoria.toString() );
		 	        values.put("Name", dato.Nome );
		 	       // values.put("datas", this.getCurrentDateTime().format("%d/%m/%y %T"));
		 	       valuesList.add(values);
	        	}	 	        
	        }
	        
	        _dbHelper.insertDataIntoTable("MenuItems", valuesList);
	       
	        return true;
        
    	}
        catch(SQLException sqlerror)
        {
	        Log.v("ERROR in addMenuItems ", sqlerror.getMessage());	
	        return false;
        }
		
	}//fM
	
	
	 public ArrayList<myCategoria> getCategoriesFromDb() {
		 
		 	Log.i("In getCategories" ,"Trying to read getCategories from database");
	        ArrayList<myCategoria> result = new ArrayList<myCategoria>();

	        //SQLiteDatabase database = getReadableDatabase();
	        Cursor cursor = _dbHelper.ExecSql("SELECT * FROM Categories" );
	        if(cursor == null)
	        	return result;
	        
	        myCategoria s;
	        cursor.moveToFirst();
	        
	        while (!cursor.isAfterLast()) 
	        {
	            s = new myCategoria( cursor.getString(0) ,cursor.getString(1)  );
	            result.add(s);

	            cursor.moveToNext();
	        }
	        cursor.close();
	       

	        if (result.isEmpty()) {
	            Log.i("In getCategories" ,"Reading  getCategories from database failed");
	            result = null;
	        } else {
	        	Log.i("In getCategories" ,"Reading getCategories from database succeeded");
	        }

	        return result;
	    }
	
	 //*******
	 


    
    public synchronized List<myMenu> getMenuItemsFromDb()
    {
    	    	
    	ArrayList<myMenu> retList = new ArrayList<myMenu>();
    	
    	try{
    		 
	        Log.d("In readDataFromItems","Start");
	
	        Cursor crsr = _dbHelper.ExecSql("Select * from MenuItems");
	        if(crsr == null)
	        	return retList;

	        Log.v("Select Query result in readDataFromItems", String.valueOf(crsr.getCount()) );

	        
	        myMenu cat = null;
	       
	        crsr.moveToFirst();
	        for(int i=0; i < crsr.getCount(); i++)
	        {
	        	cat = new myMenu(UUID.fromString(crsr.getString(0)),crsr.getString(1), crsr.getString(2)  );
	        	retList.add(cat);

	        	crsr.moveToNext();
	        }
	        crsr.close();

	        return retList;

    	}
        catch(SQLException sqlerror)
        {	
	        Log.v("Read DB ERROR in readDataFromItems", sqlerror.getMessage());
	        return retList;
        }
    	    
    }//fM
    
	
	
}//fC
